package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.dao.impl.activity.feast.FeastUserDataDaoImpl;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.feast.config.ShanHeFeastConfig;
import com.yanqu.road.entity.activity.feast.data.FeastUserData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.feast.ShanHeFeastMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class ShanHeFeastRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {
        ShanHeFeastConfig config = ShanHeFeastMgr.getConfig();
        if(config != null){
            // 背包补发, 用灵感点做key
            int itemKey = config.getFEAST_ROLL_ITEM_ID();
            int activityId = config.getActivityInfo().getActivityId();
            long recycleTime = config.getActivityInfo().getEndShowTime() * 1000L + 600 * 1000;
            List<String> pList = new ArrayList<>();
            pList.add(String.valueOf(activityId));
            pList.add(config.getActivityInfo().getActivityName());
            ActivityItemRecycleMgr.addRecycleItemData(itemKey, activityType, 5, recycleTime, pList);
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.Feast.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        if(data.getRecycleType() == 5){
            int activityId = Integer.parseInt(data.getParamList().get(0));
            FeastUserData userData = new FeastUserDataDaoImpl().getFeastUserData(activityId, userId);
            if(userData == null || userData.getTempReward() == null || userData.getTempReward().isNothing()){
                return;
            }
            Property reward = userData.getTempReward();
            userData.setTempReward(new Property());
            boolean result = new FeastUserDataDaoImpl().update(userData);
            if(!result){
                log.info("山河乐宴背包补发失败 activityId{}, update user {} fail", activityId, userId);
                return;
            }

            //邮件
            String language = UserMgr.getUserInfo(userId).getLanguage();
            String activityName = ServerLanguageMgr.getContent(data.getParamList().get(1), language);
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_BAG_REISSUE_MAIL_TITLE_1, language), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.COMMON_BAG_REISSUE_MAIL_CONTENT_1, language), activityName);
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(reward), mailContent, mailTitle);
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
